<template>
  <div class="clientType">
    <div class="title-container">
      <div class="left-box">
        <el-form ref="form" :model="searchForm" :inline="true" class="formClass">
          <el-form-item>
            <el-input
              prefix-icon="el-icon-search"
              type="input"
              placeholder="请输入客户名称"
              v-model="searchForm.customerName"
              @clear="clearSearch"
              clearable
              style="margin-right: 10px;"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="searchClick">搜索</el-button>
          </el-form-item>
        </el-form>
      </div>
    </div>
    <global-card>
      <template #leftTitle>
        <el-button type="success" icon="el-icon-refresh" @click="refresh">刷新</el-button>
      </template>
      <template #content>
        <el-table
          v-loading="loading"
          :data="tableData"
          border
          style="width: 100%"
          class="elTable"
        >
          <el-table-column type="index" label="序号" width="80" align="center"/>
          <el-table-column prop="customerCode" label="客户编码" align="center"/>
          <el-table-column prop="customerName" label="客户名称" align="center"/>
          <el-table-column prop="customerPhone" label="客户手机号 " align="center"/>
          <el-table-column prop="topUpAmount" label="充值金额 " align="center"/>
          <el-table-column prop="accountBalance" label="账户余额 " align="center"/>
          <el-table-column prop="" label="审核状态 " align="center">
            <template slot-scope="scope">
              {{ topUpStatusData[scope.row.topUpStatus].name}}
            </template>
          </el-table-column>
          <el-table-column prop="topUpMessage" label="审核意见 " align="center"/>
          <el-table-column prop="createTime" label="充值时间 " align="center"/>
          <el-table-column prop="createByName" label="创建人  " align="center"/>
          <el-table-column label="创建时间" align="center" prop="createTime" width="170">
            <template slot-scope="scope">
              {{ scope.row.createTime}}
            </template>
          </el-table-column>
          <el-table-column label="操作" width="120" align="center">
            <template #default="{ row }">
              <perButton label="审核" type="primary" size="mini" perms="PROTOCOL_ADD"
                         @click="examineClick(row)"></perButton>
            </template>
          </el-table-column>
        </el-table>
      </template>
      <template #footer>
        <pagination
          class="pagination"
          :total="count"
          :page.sync="pageIndex"
          :limit.sync="pageSize"
          @pagination="pagination"
        />
      </template>
    </global-card>

    <yc-dialog
      ref="ycDialog"
      :dialogTitle="dialogTitle"
      :centerTitle="dialogCenterTitle"
    >
      <template #content>
        <div class="dialog-content">
          <el-form
            :model="newModel"
            :rules="rules"
            ref="agreementModelRef"
            label-width="110px"
          >
            <el-form-item label="充值金额" prop="topUpAmount">
              <el-input v-model="newModel.topUpAmount" :disabled="true"/>
            </el-form-item>
            <el-form-item label="充值审核" prop="topUpStatus">
              <el-select v-model="newModel.topUpStatus">
                <el-option
                  v-for="item in topUpStatusData2"
                  :key="item.id"
                  :label="item.name"
                  :value="item.id"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="审核意见" prop="topUpMessage">
              <el-input v-model="newModel.topUpMessage"/>
            </el-form-item>
          </el-form>
        </div>
      </template>
      <template #footer>
        <el-button v-if="newModel.topUpStatus == '1'" type="primary" :loading="addOrUpdateLoading" @click="dialogSave(1)">通过</el-button>
        <el-button v-else type="primary" :loading="addOrUpdateLoading" @click="dialogSave(2)">不通过</el-button>
        <el-button @click="$refs.ycDialog.dialogVisible = false">取消</el-button>
      </template>
    </yc-dialog>

    <global-message
      v-if="isGlobalMessageShow"
      @receive="receive"
      :message="message"
    />
  </div>
</template>

<script>
  import ycDialog from "@/components/pageDialog"
  import perButton from "@/components/permissionButton"
  import {
    queryPageTopUp,
    topUpAmountDone,
    topUpAmountReject
  } from "@/api/topupManagement/clientTopup"

  export default {
    components: {
      ycDialog,
      perButton
    },
    data() {
      return {
        tableData: [],
        pageIndex: 1,
        pageSize: 10,
        count: 0,
        topUpStatusData: [
          { id: 0, name: "待审核" },
          { id: 1, name: "审核通过" },
          { id: 2, name: "审核不通过" }
        ],
        topUpStatusData2: [
          { id: 1, name: "审核通过" },
          { id: 2, name: "审核不通过" }
        ],
        searchForm: {
          customerName: ""
        },

        detailsDisabled: false,
        isGlobalMessageShow: false,

        dialogCenterTitle: "",
        dialogTitle: "",
        message: "",

        loading: false,
        newModel: {
          id: "",
          topUpAmount: "",
          topUpStatus: 1,
          topUpMessage: "",
        },
        rules: {
          topUpMessage: [
            { required: true, message: "请输入审核意见", trigger: "blur" }
          ],
          topUpStatus: [
            { required: true, message: "请选择", trigger: "change" }
          ],
        },
        userData: [],
        addOrUpdateLoading: false
      }
    },
    created() {
      this.getData()
    },
    methods: {
      async searchClick() {
        this.getData()
      },
      clearSearch() {
        this.getData()
      },
      async getData() {
        const { customerName } = this.searchForm
        this.loading = true
        const data = await queryPageTopUp({
          pageIndex: this.pageIndex,
          pageSize: this.pageSize,
          topUpStatus: 0,
          customerName
        })
        this.tableData = data.object.data
        this.count = data.object.count
        this.loading = false
      },
      pagination(pageItem) {
        this.getData(pageItem.page, pageItem.limit)
      },

      // 审核
      examineClick(row) {
        this.dialogCenterTitle = "审核"
        this.dialogTitle = "审核"

        this.newModel.id = row.id
        this.newModel.topUpAmount = row.topUpAmount
        // this.newModel.topUpStatus = row.topUpStatus
        this.newModel.topUpMessage = row.topUpMessage

        this.detailsDisabled = false
        this.addOrUpdateLoading = false
        this.$refs.ycDialog.dialogVisible = true
      },

      async dialogSave(type) {
        this.$refs.agreementModelRef.validate(async (v) => {
          if (v) {
            this.addOrUpdateLoading = true
            if(type == 1) {
              const { status, message } = await topUpAmountDone({ ...this.newModel })
              if (status == "200") {
                this.$message({
                  message: message,
                  type: "success"
                })
                this.getData()
                this.$refs.ycDialog.dialogVisible = false
              }
            } else {
              const { status, message } = await topUpAmountReject({ ...this.newModel })
              if (status == "200") {
                this.$message({
                  message: message,
                  type: "success"
                })
                this.getData()
                this.$refs.ycDialog.dialogVisible = false
              }
            }

            this.addOrUpdateLoading = false
          }
        })
      },

      receive() {
        this.isGlobalMessageShow = false
      },

      refresh() {
        this.searchForm = {
          agreementName: "",
          agreementType: ""
        }
        this.getData()
      }
    }
  }
</script>
<style lang="scss" scoped>
  .clientType {
    .title-container {
      display: flex;
      justify-content: flex-end;
      margin-right: 20px;

      .left-box {
        display: flex;

        .formClass {
          /*display: flex;*/
          /*margin-right: 20px;*/

          .el-form-item {
            margin-top: 20px;
            margin-bottom: 0;

            .el-input {
              width: 216px;
            }

            .el-select {
              width: 171px;
              margin-left: 10px;
            }

            .el-date-editor {
              width: 336px;
              margin-left: 10px;
            }
          }
        }

        /*.el-button {*/
        /*  // width: 42px;*/
        /*  height: 36px;*/
        /*  margin-top: 20px;*/
        /*}*/
      }
    }
  }
</style>
